package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.mall.MallActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.config.goods.MallWheelResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.player.UserActivityScore;
import com.yanqu.road.entity.player.UserMallBuyCount;
import com.yanqu.road.entity.servercenter.MallRebate;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.pb.mall.MallProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.MallActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class MallPb {

	public static MallProto.MallBuyCountTempMsg.Builder parseMallBuyCountTempMsg(UserMallBuyCount buyCount) {
		if(null == buyCount){
			return null;
		}
		MallProto.MallBuyCountTempMsg.Builder msg = MallProto.MallBuyCountTempMsg.newBuilder();
		msg.setMallId(buyCount.getMallId());
		msg.setCount(buyCount.getCount());
		msg.setLastUpdateTime(buyCount.getLastUpdateTime());
		return msg;
	}

	public static MallProto.MallBuyCountListMsg.Builder parseMallBuyCountListMsg(List<UserMallBuyCount> buyCountList) {
		MallProto.MallBuyCountListMsg.Builder listMsg = MallProto.MallBuyCountListMsg.newBuilder();
		if(null != buyCountList) {
			for (UserMallBuyCount buyCount : buyCountList) {
				MallProto.MallBuyCountTempMsg.Builder msg = parseMallBuyCountTempMsg(buyCount);
				if (null != msg) {
					listMsg.addMallBuyCountList(msg);
				}
			}
		}
		return listMsg;
	}

	public static MallProto.MallActivityTempMsg.Builder parseMallActivityTempMsg(MallActivityInfo mallActivityInfo, String language){
		if(null == mallActivityInfo){
			return null;
		}
		MallProto.MallActivityTempMsg.Builder msg = MallProto.MallActivityTempMsg.newBuilder();
		msg.setActivityId(mallActivityInfo.getActivityId());
		msg.setActivityName(ServerLanguageMgr.getContent(mallActivityInfo.getActivityName(), language));
		msg.setBeginTime(mallActivityInfo.getBeginTime());
		msg.setEndTime(mallActivityInfo.getEndTime());
		msg.setType(mallActivityInfo.getType());
		msg.setChildType(mallActivityInfo.getChildType());
		for(MallInfo mallInfo : filterByExtendParam(mallActivityInfo)){
			CommonProto.MallTempMsg.Builder mallMsg = CommonPb.parseMallTempMsg(mallInfo, language);
			if(null != mallMsg){
				msg.addGoodsList(mallMsg);
			}
		}
		return msg;
	}

	public static Collection<MallInfo> filterByExtendParam(MallActivityInfo mallActivityInfo) {
		if(mallActivityInfo.getType() != eActivityType.CricketMallActivity.getValue()){
			return mallActivityInfo.getMallInfoMap().values();
		}
		List<MallInfo> result = new ArrayList<>();
		try{
			ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(mallActivityInfo.getActivityId());
			ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
			if(activityInfo != null && serverInfo != null){
				MallWheelResult wheelResult = MallActivityMgr.calWheel(activityInfo, serverInfo);
				for (MallInfo mallInfo : mallActivityInfo.getMallInfoMap().values()) {
					if(mallInfo.getReSetTime() < 1 || "0".equals(mallInfo.getExtendParam())){
						result.add(mallInfo);
					}else {
						int allRound = MallActivityMgr.getAllRound(mallInfo.getReSetTime(), mallActivityInfo.getMallInfoMap().values());
						int round = Integer.parseInt(mallInfo.getExtendParam());
						if(round == wheelResult.calRound(allRound, mallInfo.getReSetTime())){
							result.add(mallInfo);
						}
					}
				}
			}
		}catch (Exception e){

		}
		return result;
	}

	public static MallProto.MallActivityListMsg.Builder parseMallActivityListMsg(List<MallActivityInfo> mallActivityInfoList, String language){
		MallProto.MallActivityListMsg.Builder listMsg = MallProto.MallActivityListMsg.newBuilder();
		for(MallActivityInfo mallActivityInfo : mallActivityInfoList){
			MallProto.MallActivityTempMsg.Builder activityMsg = parseMallActivityTempMsg(mallActivityInfo, language);
			if(null != activityMsg){
				listMsg.addActivityList(activityMsg);
			}
		}
		return listMsg;
	}

	public static MallProto.SyncActivityScoreMsg.Builder parseActivityScore(List<UserActivityScore> userActivityScoreList) {
		MallProto.SyncActivityScoreMsg.Builder respMsg = MallProto.SyncActivityScoreMsg.newBuilder();
		for (UserActivityScore score : userActivityScoreList) {
			MallProto.ActivityScoreTemp.Builder activityScoreTemp = MallProto.ActivityScoreTemp.newBuilder();
			activityScoreTemp.setActivityId(score.getActivityId());
			activityScoreTemp.setScoreGoodsId(score.getGoodsId());
			activityScoreTemp.setTodayScore(score.getDayScore());
			activityScoreTemp.setTotalScore(score.getTotalScore());
			activityScoreTemp.setLastResetTime(score.getLastResetTime());
			respMsg.addActivityScore(activityScoreTemp);
		}
		return respMsg;
	}

	public static MallProto.MallRebateTempMsg.Builder parseMallRebateTempMsg(MallRebate mallRebate){
		if(null == mallRebate){
			return null;
		}
		MallProto.MallRebateTempMsg.Builder msg = MallProto.MallRebateTempMsg.newBuilder();
		msg.setMallId(mallRebate.getMallId());
		msg.setRebateRatio(mallRebate.getRebateRatio());
		return msg;
	}

	public static MallProto.MallRebateListMsg.Builder parseMallRebateListMsg(List<MallRebate> mallRebateList){
		MallProto.MallRebateListMsg.Builder listMsg = MallProto.MallRebateListMsg.newBuilder();
		if(null != mallRebateList){
			for(MallRebate mallRebate : mallRebateList){
				MallProto.MallRebateTempMsg.Builder msg = parseMallRebateTempMsg(mallRebate);
				if(null != msg){
					listMsg.addDataList(msg);
				}
			}
		}
		return listMsg;
	}
}
